package com.sparkseries.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sparkseries.common.util.domain.Result;
import com.sparkseries.common.util.exception.BusinessException;
import com.sparkseries.common.util.tool.ObjectConvertTool;
import com.sparkseries.dao.SysLogMapper;
import com.sparkseries.domain.SysLog;
import com.sparkseries.domain.vo.SysLogVO;
import com.sparkseries.dto.SysLogDTO;
import com.sparkseries.service.SysLogService;
import com.sparkseries.service.SysNoticeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
@Slf4j
public class SysLogServiceImpl implements SysLogService {

    @Resource
    private SysLogMapper sysLogMapper;

    /**
     * 保存日志
     *
     * @param sysLog 日志传输类
     */
    @Override
    public void save(SysLog sysLog) {
        long id = IdWorker.getId();
        sysLog.setId(id);
/*        int flag;
        if (sysLog.getLogType().equals("用户管理")) {
            flag = sysLogMapper.saveUserLog(sysLog);
        } else if (sysLog.getLogType().equals("机构管理")) {
            flag = sysLogMapper.saveInstitutionLog(sysLog);
        } else {
            flag = sysLogMapper.save(sysLog);
        }*/
        if (sysLogMapper.save(sysLog) <= 0) {
            log.error("日志保存失败，日志信息:{}", sysLog);
            throw new BusinessException("日志保存时发生异常");
        }
    }

    /**
     * 日志归档
     *
     * @param date 日期
     */
    @Override
    public void deleteByDate(Date date) {
        if (sysLogMapper.deleteByDate(date) <= 0) {
            if(Objects.isNull(sysLogMapper.listLog(null,null))){
                log.error("日志归档失败");
                throw new BusinessException("日志归档时发生异常");
            }
        }
    }

    /**
     * 修改日志级别或类型
     *
     * @param logId    日志id
     * @param logLevel 日志级别
     * @param logType  日志类型
     * @return 默认响应类
     */
    @Override
    public Result<?> updateLog(Long logId, String logLevel, String logType) {
        if (sysLogMapper.updateLog(logId, logLevel, logType) <= 0) {
            log.error("修改日志失败,日志ID:{}", logId);
            throw new BusinessException("修改日志时发生异常");
        }
        return Result.ok("修改成功");
    }

    /**
     * 日志列表
     *
     * @return 日志响应类集合
     */
    @Override
    public Result<List<SysLogVO>> listLog(SysLogDTO sysLogDTO, String detail) {
        SysLog sysLog = ObjectConvertTool.originConvert(sysLogDTO, SysLog.class);
        List<SysLog> sysLogs = sysLogMapper.listLog(sysLog, detail);
        List<SysLogVO> sysLogVOs = convertToSysLogVOs(sysLogs);
        return Result.ok("查找成功", sysLogVOs);
    }

    /**
     * 查找日志
     *
     * @param logId 日志id
     * @return 日志响应类
     */
    @Override
    public Result<SysLogVO> getLog(Long logId) {
        return Optional.ofNullable(sysLogMapper.getLog(logId))
                .map(sysLog -> {
                    log.debug("成功获取日志，日志ID: {}", logId);
                    return Result.ok("查询成功", ObjectConvertTool.originConvert(sysLog, SysLogVO.class));
                })
                .orElseGet(() -> {
                    log.warn("未找到相关日志，日志ID: {}", logId);
                    return Result.error("日志未找到");
                });
    }

    /**
     * 导出日志
     *
     * @return 默认响应类
     */
    @Override
    public Result<?> exportLogs() {
        try {
            List<SysLog> sysLogs = sysLogMapper.listLog(null,null);
            List<SysLogVO> sysLogVOs = convertToSysLogVOs(sysLogs);
            // TODO: 实现导出工具
            return Result.ok("导出成功");
        } catch (Exception e) {
            log.error("导出日志失败", e);
            throw new BusinessException("导出日志失败", e);
        }
    }

    /**
     * 提取的通用转换方法，将 SysLog 列表转换为 SysLogVO 列表
     *
     * @param sysLogs SysLog 列表
     * @return SysLogVO 列表
     */
    private List<SysLogVO> convertToSysLogVOs(List<SysLog> sysLogs) {
        return ObjectConvertTool.originsConvert(sysLogs, SysLogVO.class);
    }
}